package logic

import (
	"context"
	"login-guard/mysql/models"
	"login-guard/pkg"

	"login-guard/loginrpc/internal/svc"
	"login-guard/loginrpc/loginrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *loginrpc.LoginRequest) (*loginrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	data := models.User{
		Username: in.Username,
		Password: pkg.Md5(in.Password),
	}
	if err := data.LoginUser(models.DB); err != nil {
		return nil, err
	}
	return &loginrpc.LoginResponse{
		UserId: int64(data.ID),
	}, nil
}
